﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using MyGeneration.dOOdads;

namespace WebUI.EditPages
{
    public partial class PayOne : BasePage
    {
        int id = 0;
        Invoices ivc;

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if(string.IsNullOrEmpty(Request.QueryString[ID]))
            {
                CloseDlg(0);
                return;
            }

            id = int.Parse(Request.QueryString[ID]);
            ivc = new Invoices();
            ivc.LoadByPrimaryKey(id);
            if (ivc.RowCount <= 0)
            {
                CloseDlg(0);
                return;
            }

            if (!IsPostBack)
            {
                PaymentMethod methods = PaymentMethod.GetAllMethods();
                Utilities.LoadDropDownListNoSelect(ddlPaymentMethod, methods.DefaultView, PaymentMethod.ColumnNames.Name, PaymentMethod.ColumnNames.ID);
                methods.Filter = PaymentMethod.ColumnNames.Default + "=1 and " + PaymentMethod.ColumnNames.MethodType + "='B'";
                if (methods.RowCount > 0)
                    ddlPaymentMethod.SelectedValue = methods.ID.ToString();
                txtPayAmt.Text = (ivc.TotalAmount - ivc.PaidAmount).ToString();
                txtTotalAmt.Text = string.Format("{0:C}", ivc.TotalAmount - ivc.PaidAmount);
            }
        }

        protected void btnPay_OnClick(object sender, EventArgs e)
        {
            TransactionMgr tx = TransactionMgr.ThreadTransactionMgr();
            try
            {
                tx.BeginTransaction();

                InvoicePaymentHistories hist = new InvoicePaymentHistories();
                decimal amt = decimal.Parse(txtPayAmt.Text);
                amt = Math.Min(ivc.TotalAmount - ivc.PaidAmount, amt);

                if (amt > 0)
                {
                    int methodID = int.Parse(ddlPaymentMethod.SelectedValue);

                    ivc.Pay(amt, methodID, SessionUser.ID);
                    hist.AddPayment(ivc.ID, methodID, amt, DateTime.Now, SessionUser.ID);

                    if (!ivc.IsColumnNull("CustomerID") && ivc.CustomerID > 0)
                    {
                        POSSystem.BLL.Customers cust = new POSSystem.BLL.Customers();
                        cust.UpdateCustomerAmt(ivc.CustomerID, 0, amt);
                    }

                    ivc.Save();
                    hist.Save();
                }

                tx.CommitTransaction();
                CloseDlg(id);
            }
            catch (Exception ex)
            {
                tx.RollbackTransaction();
                ErrorLog.WriteToFile("PayOne.aspx.cs", "btnPay_OnClick", ex.StackTrace);
                ((Popup)Master).ShowErrorMessage("Error: " + ex.Message);
            }
        }
    }
}